import GlobalEvent from "../event/GlobalEvent";
import XlsxData from "../xlsx/XlsxData";
import XlsxDataManager from "../xlsx/XlsxDataManager";
import LangManager from "./LangManager";

const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@requireComponent(cc.Label)
export default class GameText extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    dataName: string = 'ui_data'

    @property
    dataID: string = '1'

    @property
    dataIndex: number = 0;

    onLoad() {
        this.setText()

        GlobalEvent.instance().on(GlobalEvent.CHANGE_LANG, this.setText, this)

    }
	
	onDestroy(){
		GlobalEvent.instance().off(GlobalEvent.CHANGE_LANG, this.setText, this)
	}


    setText() {
        if (!this.label) {
            this.label = this.node.getComponent(cc.Label);
        }
        if (this.label) {
            let data: XlsxData = XlsxDataManager.instance().get(this.dataName)
            if (data) {
                let langID = data.getValue(this.dataID, this.dataIndex);
                if (langID) {
                    let str = LangManager.instance().getLocalString(langID)
                    if (str) {
                        this.label.string = str;
                    }

                }
            }

        }
    }



}
